package com.taiyi.crm.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.taiyi.crm.mapper.PermissionMapper;
import com.taiyi.crm.mapper.RoleMapper;
import com.taiyi.crm.mapper.RolePermissionMapper;
import com.taiyi.crm.pojo.entity.Permission;
import com.taiyi.crm.pojo.entity.Role;
import com.taiyi.crm.pojo.entity.RolePermission;
import com.taiyi.crm.pojo.vo.LayUISelectMVO;
import com.taiyi.crm.service.IRoleService;
import com.taiyi.crm.util.LayUITableResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class RoleServiceImpl implements IRoleService{
    @Autowired
    private RoleMapper roleMapper;
    @Autowired
    private PermissionMapper permissionMapper;

    @Autowired
    private RolePermissionMapper rolePermissionMapper;

    @Override
    public LayUITableResult list(Integer page, Integer limit, Role role) {
        //1、使用PageHelper插件设置分页
        PageHelper.startPage(page, limit);
        //2、执行查询
        List<Role> list = roleMapper.selectByPage(role);
        //3、使用PageInfo对结果进行包装
        PageInfo pageInfo = new PageInfo(list);
        Integer count = (int) pageInfo.getTotal();//得到总数量

        return  LayUITableResult.ok(count, list);
    }

    @Override
    public void deleteById(Integer id) {
        roleMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteAll(String ids) {
        String[] idArray = ids.split(",");
        roleMapper.deleteAll(idArray);
    }

    @Override
    public void insert(Role role, String permissions) {
        // 将角色插入到数据库中
        int count = roleMapper.insert(role);
        // 将角色-权限多对多关系放到角色-权限role_permission表中
        // 3,4,8
        String[] permissionIds = permissions.split(",");
        for (String permissionId : permissionIds) {
            RolePermission rolePermission =
                    new RolePermission(role.getId(),
                            Integer.parseInt(permissionId));
            rolePermissionMapper.insert(rolePermission);
        }
    }

    @Override
    public Role selectByPrimaryKey(Integer roleId) {
        return roleMapper.selectByPrimaryKey(roleId);
    }

    @Override
    public Map<String, Object> selectRoleAndPermisssions(Integer roleId) {
        Map<String, Object> map = new HashMap<>();
        // 查询role信息放到map中
        Role role = roleMapper.selectByPrimaryKey(roleId);
        map.put("role", role);
        // 查询所有的权限放到map中
        List<Permission> permissions = permissionMapper.selectByPage(new Permission());
        List<LayUISelectMVO> list = new ArrayList<>();
        for (Permission permission : permissions) {
            LayUISelectMVO layUISelectMVO = new LayUISelectMVO();
            layUISelectMVO.setId(permission.getId());
            layUISelectMVO.setName(permission.getName());
            layUISelectMVO.setStatus(1);
            list.add(layUISelectMVO);
        }
        map.put("allPermissions", list);
        // 这个角色Role对应的所有权限id的集合放到map中
        List<Integer> roleSelectedPermissionIds = rolePermissionMapper.selectPermissionIdsByRoleId(roleId);
        map.put("selectIds", roleSelectedPermissionIds.toArray());

        return map;
    }

    @Override
    public void update(Role role, Integer[] permissions) {
        // 将角色更新到数据库中
        int count = roleMapper.updateByPrimaryKey(role);
        // 删除这个角色下面原来的权限
        rolePermissionMapper.deleteByRoleId(role.getId());
        // 将角色-权限多对多关系放到角色-权限表中
        for (Integer permissionId : permissions) {
            RolePermission rolePermission = new RolePermission(role.getId(), permissionId);
            rolePermissionMapper.insert(rolePermission);
        }
    }

    @Override
    public List<LayUISelectMVO> selectAllRoles() {
        List<Role> roles = roleMapper.selectByPage(new Role());
        List<LayUISelectMVO> list = new ArrayList<>();
        for (Role role : roles) {
            LayUISelectMVO layUISelectMVO = new LayUISelectMVO();
            layUISelectMVO.setId(role.getId());
            layUISelectMVO.setName(role.getSn());
            layUISelectMVO.setStatus(1);
            list.add(layUISelectMVO);
        }

        return list;
    }
}